<template>
  <!-- 头部 -->
  <header class="header">
    <img src="../../assets/logo4.png" alt="Logo" class="logo" />
    <nav class="navbar">
      <!-- 用户信息下拉 -->
      <div class="user-dropdown" @mouseenter="showDropdown = true" @mouseleave="showDropdown = false">
        <span class="nav-link user-container">
          <el-icon><User /></el-icon>
          <span>{{ currentUser.name || currentUser.username }}</span>个人中心
        </span>
        <div v-show="showDropdown" class="dropdown-menu">
          <router-link to="/admin/user" class="dropdown-item">查看个人信息</router-link>
          <a href="#" @click.prevent="logout" class="dropdown-item">退出登录</a>
        </div>
      </div>
    </nav>
  </header>

  <!-- 主内容区域（整个 flex 容器包含菜单和右侧内容） -->
  <div style="display: flex">
    <!-- 左侧菜单 -->
    <div style="width: 200px; border-right: 1px solid #ddd; height: calc(100vh - 60px);">
      <el-menu router>
        <el-menu-item index="/admin/Home">系统首页</el-menu-item>
        <el-menu-item index="Question">题库设计</el-menu-item>
      </el-menu>
    </div>

    <!-- 右侧内容（子路由在此渲染） -->
    <div style="flex: 1; width: 0; background-color: #e1e4eb; padding: 10px;">
      <router-view></router-view>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { User } from '@element-plus/icons-vue'
import { getToken, parseJwt } from '@/utils/auth'
import router from '@/router/index'
export default {
  name: 'AdminManagement',
  components: {
    User
  },
  data() {
    return {
      showDropdown: false,
      currentUser: {}
    }
  },
  mounted() {
    this.loadUserInfo()
  },
  methods: {
    loadUserInfo() {
      const userInfoStr = localStorage.getItem('userInfo')
      if (userInfoStr) {
        try {
          const userInfo = JSON.parse(userInfoStr)
          this.currentUser = userInfo
        } catch (e) {
          console.error('解析用户信息失败', e)
        }
      } else {
        console.warn('⚠️ 无法获取用户信息')
        this.$router.push('/login') // 如果没有 userInfo，直接跳回登录页
      }
    },
    logout() {
      // 清除 token 和用户信息
      localStorage.removeItem('token')
      localStorage.removeItem('userInfo')

      // 跳转到登录页
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped>
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 20px;
  background-color: #2c3e50;
  color: white;
}

.navbar {
  display: flex;
  align-items: center;
  gap: 15px;
  position: relative;
}

.nav-link {
  text-decoration: none;
  color: white;
  font-weight: bold;
  transition: color 0.3s ease;
  cursor: pointer;
}
.logo {
  width: 30px;
  height: auto;
}
/* 下拉菜单容器 */
.user-dropdown {
  position: relative;
  display: inline-block;
}

/* 用户名区域样式 */
.user-container {
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
}

.user-container .el-icon {
  font-size: 18px;
  color: white;
}

/* 下拉菜单样式 */
.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  background-color: white;
  border: 1px solid #ccc;
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  z-index: 1000;
  min-width: 120px;
}

.dropdown-item {
  display: block;
  padding: 10px 15px;
  text-decoration: none;
  color: #333;
  transition: background-color 0.2s;
}

.dropdown-item:hover {
  background-color: #f0f0f0;
}
</style>